package org.jetlinks.pro.things.impl.rpc;

import lombok.AllArgsConstructor;
import org.jetlinks.core.message.ThingMessage;
import org.jetlinks.core.things.ThingRpcSupport;
import reactor.core.publisher.Flux;

import java.util.function.Function;


/**
 * 物消息转换器。用于RPC执行前后，将消息转换为新消息
 *
 * @author zhouhao
 * @since 1.12
 */
@AllArgsConstructor
public class ConverterThingsRpcSupport implements ThingRpcSupport {

    //RPC支持
    private final ThingRpcSupport source;

    //RPC请求消息转换
    private final Function<ThingMessage, ThingMessage> requestConverter;

    //RPC调用结果消息转换
    private final Function<ThingMessage, ThingMessage> responseConvert;

    /**
     * RPC调用
     *
     * @param message RPC请求消息
     * @return 多个RPC的调用结果
     */
    @Override
    public Flux<? extends ThingMessage> call(ThingMessage message) {
        return source
            .call(requestConverter.apply(message))
            .map(responseConvert);
    }
}
